package com.kx.center.service.impl;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.apache.commons.lang.math.NumberUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.kx.center.common.MyQueue;
import com.kx.center.entity.MsgNotice;
import com.kx.center.entity.MsgSend;
import com.kx.center.mappers.MsgNoticeMapper;
import com.kx.center.service.CallBackInterface;
import com.kx.center.service.MsgNoticeService;
import com.kx.center.util.SendJpushThread;

/**
 * @description 通知公告服务类
 * @version 1.0
 * @author Scott
 * @date 2016年1月6日 上午11:04:48
 */
@Service
public class MsgNoticeServiceImpl implements MsgNoticeService {

	@Autowired
	MsgNoticeMapper noticeMapper;

	@Override
	public List<MsgNotice> findNoticeListByPage(int companyId,int pageNow, int pageSize) {

		return noticeMapper.findNoticeListByPage(companyId,(pageNow - 1) * pageSize, pageSize);
	}

	@Override
	public int getPageCount(int companyId) {

		return noticeMapper.getPageCount(companyId);
	}

	@Override
	public List<MsgSend> findAppNoticeListByPage(int pageNow, int pageSize, int appAccountId) {
		return noticeMapper.findAppNoticeListByPage((pageNow - 1) * pageSize, pageSize, appAccountId);
	}

	@Override
	public int getAppNoticePageCount(int appAccountId) {

		return noticeMapper.getAppNoticePageCount(appAccountId);
	}

	@Override
	public void changeSendStatus(int sendId) {
		
		//查询当前消息的状态，如果是已查看 就不再修改状态和更改查看人数
		int status = noticeMapper.findSendStatus(sendId);
		
		if(status == 1){
			noticeMapper.changeSendStatus(sendId);
			// 查询发送题对应的消息体id
			int noticeId = this.findNoticeMsgId(sendId);
			if (noticeId > 0) {
				this.changeNoticeCheckedNum(noticeId);
			}
		}
		
	}

	@Override
	public int findNoticeMsgId(int sendId) {
		return noticeMapper.findNoticeMsgId(sendId);
	}

	@Override
	public void changeNoticeCheckedNum(int noticeId) {
		noticeMapper.changeNoticeCheckedNum(noticeId);
	}

	@Override
	public List<MsgSend> findUncheckedMsg(int appAccountId) {

		return noticeMapper.findUncheckedMsg(appAccountId);
	}
	
	

	@Override
	public void sendNoticeMsg(MsgNotice msgNotice, ArrayList<HashMap<String, Object>> data) {
		if(data == null || data.size() <=0){
			//没有传入接受者，查询所有
			if(msgNotice.getCompanyId() <=0 ){
				return ;
			}
			data = this.findAllAppAccount(msgNotice.getCompanyId());
		}
		
		// 发送公告 1.先创建消息体
		msgNotice.setSendNum(data.size());
		this.addNoticeMsg(msgNotice);
		// 2.将接收人添加到数据库中
		// System.out.println(msgNotice.getId());
		this.sendNoticeMsg2Account(msgNotice, data);
		// 3.放入消息队列 4.执行线程发送json消息
	}

	private void sendNoticeMsg2Account(MsgNotice msgNotice, ArrayList<HashMap<String, Object>> data) {
		if (data != null) {
			MyQueue<MsgSend> sendQueue = new MyQueue<>();
			
			for (int i=0; i<data.size(); i++) {
				MsgSend msgSend = new MsgSend();
				msgSend.setNoticeMsgId(msgNotice.getId());
				msgSend.setTitle(msgNotice.getTitle());
				msgSend.setContent(msgNotice.getContent());
				msgSend.setDiscribe(msgNotice.getDiscribe());
				msgSend.setOrderId(msgNotice.getOrderId());
				msgSend.setOrderStatus(msgNotice.getOrderStatus());
				
				msgSend.setAppAccountId(NumberUtils.toInt(data.get(i).get("id").toString()));
				msgSend.setAppAccountUserId(data.get(i).get("user_id").toString());
				msgSend.setStatus(1);
				msgSend.setType("notice");
				
				//插入数据库中
				noticeMapper.sendNoticeMsg2Account(msgSend);
				
				//加入消息队列中
				sendQueue.offer(msgSend);
			}
			//启动线程  发送jpush推送
			SendJpushThread thread = new SendJpushThread(sendQueue,new CallBackInterface() {
				
				@Override
				public void updateSendNum(int noticeId, int successNum) {
					// 回调更新推送成功人数
					noticeMapper.updateSendNum(noticeId, successNum);
					//更新消息发送状态
					noticeMapper.updateSendStatus(noticeId);
				}
			});
			Thread t = new Thread(thread);
			t.start();
		}

	}

	@Override
	public void addNoticeMsg(MsgNotice msgNotice) {
		// 发送公告
		noticeMapper.addNoticeMsg(msgNotice);
	}

	@Override
	public void updateSendNum(int noticeId, int successNum) {
		noticeMapper.updateSendNum(noticeId,successNum);
	}

	@Override
	public String findNoticeDeailById(int msgSendId) {
		// 查询公告详情
		return noticeMapper.findNoticeDeailById(msgSendId);
	}

	@Override
	public ArrayList<HashMap<String, Object>> findAllAppAccount(int companyId) {
		return noticeMapper.findAllAppAccount(companyId);
	}

	@Override
	public String findNoticeDeailWebById(int noticeId) {
		return noticeMapper.findNoticeDeailWebById(noticeId);
	}

	@Override
	public void removeNotice(int noticeId) {
		noticeMapper.removeNotice(noticeId);
	}

}
